﻿''' <summary>
''' Send SMS Automated class to send sms using the fullonsms.com service.
''' </summary>
''' <remarks></remarks>
Public Class SendSMSFullOnSMS
    Implements ISmsSender

    ''' <summary>
    ''' Do the login process for fullonsms
    ''' </summary>
    ''' <param name="userName"></param>
    ''' <param name="password"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DoLogin(ByVal userName As String, ByVal password As String) As BOResult(Of String) Implements ISmsSender.DoLogin
        Dim obj As New BOResult(Of String)
        Dim wc As New System.Net.WebClient
        Dim d As New Specialized.NameValueCollection
        d.Add("MobileNoLogin", userName)
        d.Add("LoginPassword", password)
        d.Add("RememberMe", "1")

        Dim response = wc.UploadValues("http://180.150.134.195/login.php", d)

        Dim mem2 As New IO.MemoryStream(response)

        Dim reader As New System.IO.StreamReader(mem2)
        Dim t = reader.ReadToEnd

        If t.Contains("http://180.150.134.195/home.php?Login=1") Then
            Dim cookies = wc.ResponseHeaders.Get("Set-Cookie")

            Dim match = Regex.Match(cookies, "PHPSESSID=[^;]*")
            If match.Success Then
                obj.OperationStatus = True
                obj.Result = match.Value
            End If

        Else
            obj.OperationStatus = False
            obj.Status = "Invalid Username or Password"
        End If

        'obj.Result = t
        Return obj

    End Function

    ''' <summary>
    ''' Send the SMS to the mobile number.
    ''' </summary>
    ''' <param name="userName">Mobile number registered on fullonsms.com</param>
    ''' <param name="password">Password for fullonsms.com</param>
    ''' <param name="msg">SMS to send</param>
    ''' <param name="mobileNo">Mobile number to SMS send</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SendSMS(ByVal userName As String, ByVal password As String, ByVal msg As String, ByVal mobileNo As String) As BOResult(Of String) Implements ISmsSender.SendSMS
        Dim obj As New BOResult(Of String)
        Dim o = DoLogin(userName, password)
        If o.OperationStatus Then
            Dim wc As New System.Net.WebClient
            wc.Headers.Add("Cookie", o.Result)
            Dim d As New Specialized.NameValueCollection
            d.Add("MobileNos", mobileNo)
            d.Add("Message", msg)

            Dim response = wc.UploadValues("http://180.150.134.195/home.php", d)

            Dim mem2 As New IO.MemoryStream(response)

            Dim reader As New System.IO.StreamReader(mem2)
            Dim t = reader.ReadToEnd

            If t.Contains("http://180.150.134.195/MsgSent.php") Then
                obj.Status = "SMS Sent Successfully"
                obj.OperationStatus = True
            Else
                obj.OperationStatus = False
                obj.Status = "Error Occurred while Sending"
            End If
            Return obj
        Else
            Return o
        End If

    End Function

End Class
